rm(list = ls())

## ##

library(tidyverse)
library(pbapply)
library(rdrobust)

covs <- c('turnout_party_2009_btw', 'soz_vers_beschaeftigte_share',
          'pop_density_km2', 'migration_out_share')

#### Get data ####

mu <- readRDS('data/data_municipal.rds') %>%
  filter(!is.na(treated)) %>%
  mutate(state_id = substr(ags, 1, 2)) %>% 
  filter(time_rel_period > -2)

## Relevant vars

# inc_share2 - incumbent share (only for the period right before and after census)
# inc_party_prior_to_census - incumbent prior to census

## Outcomes

outcomes <- c('turnout')

## Source helper function to tidy rdrobust output

source("code/tidy_rd.R")

#### RD With First difference as outcome ####

olist <- c("turnout")

diff_df <- pblapply(olist, function(o) {
  out <- mu %>%
    filter(time_rel_period > -2) %>%
    mutate(time_rel_period = time_rel_period + 2)  %>% 
    pivot_wider(values_from = o, names_from = 'time_rel_period', 
                id_cols = 'ags',
                names_prefix = 'o') %>%
    mutate(diff = o2  - o1) %>%  dplyr::select(ags, diff) 
  ## Rename
  colnames(out)[2] <- o
  
  ## Return this
  out
}) %>%
  reduce(left_join) %>%
  left_join(mu %>% dplyr::select(ags, pop_dec_09, applies_census,
                                 one_of(covs), state_id,
                                 inc_party_prior_to_census) %>%
              distinct(ags, .keep_all = T)) %>%
  mutate(runvar = (pop_dec_09 * -1) + 10000) %>%
  filter(applies_census == 1)

## Estimates conditional on municipal incumbent

subset_select <- !diff_df$state_id == '08' 
subset_select_cdu <- !diff_df$state_id == '08' & 
  diff_df$inc_party_prior_to_census == 'cdu_csu'
subset_select_spd <- !diff_df$state_id == '08' & 
  diff_df$inc_party_prior_to_census == 'spd'

out_muni_full <- lapply(olist, function(o) {
  
  out_muni <- rdrobust(y = diff_df[subset_select, o] %>% pull(!!o), 
                      x = diff_df$runvar[subset_select], c = 0,
                      covs = diff_df[subset_select, covs])
  out_muni_cdu <- rdrobust(y = diff_df[subset_select_cdu, o] %>% pull(!!o), 
                          x = diff_df$runvar[subset_select_cdu], c = 0,
                          covs = diff_df[subset_select_cdu, covs])
  out_muni_spd <- rdrobust(y = diff_df[subset_select_spd, o] %>% pull(!!o), 
                          x = diff_df$runvar[subset_select_spd], c = 0,
                          covs = diff_df[subset_select_spd, covs])
  
  ## Tidy
  
  r1 <- out_muni %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Full sample')
  r2 <- out_muni_cdu %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Municipal incumbent: CDU/CSU')
  r3 <- out_muni_spd %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Municipal incumbent: SPD')
  
  rbind(r1, r2, r3)
}) %>% 
  reduce(rbind) %>% 
  mutate(election = 'Municipal\nelections')

## Get federal election data ##

bt <- readRDS('data/data_federal.rds') %>%
  filter(!is.na(treated)) %>% 
  filter(year > 2012)

## Prep; first differences

olist <- c("turnout_party")

diff_df <- pblapply(olist, function(o) {
  out <- bt %>%
    filter(year > 2012) %>%
    pivot_wider(values_from = o, names_from = 'year', id_cols = 'ags',
                names_prefix = 'o') %>%
    mutate(diff = o2017  - o2013) %>%  dplyr::select(ags, diff) 
  ## Rename
  colnames(out)[2] <- o
  
  ## Return this
  out
}) %>%
  reduce(left_join) %>%
  left_join(bt %>% dplyr::select(ags, pop_dec_09, applies_census,
                                 one_of(covs), state_id) %>%
              distinct(ags, .keep_all = T)) %>%
  mutate(runvar = (pop_dec_09 * -1) + 10000) %>%
  filter(applies_census == 1) %>% 
  filter(!state_id == '08')

## Merge muni incumbent to this 

diff_df <- diff_df %>% 
  left_join(mu %>% 
              dplyr::select(ags, inc_party_prior_to_census) %>% 
              distinct(ags, .keep_all = T))

## Subsets (as above)

subset_select <- !diff_df$state_id == '08' 
subset_select_cdu <- !diff_df$state_id == '08' & 
  diff_df$inc_party_prior_to_census == 'cdu_csu'
subset_select_spd <- !diff_df$state_id == '08' & 
  diff_df$inc_party_prior_to_census == 'spd'

## Optimal BWs estimated separately

out_btw_full <- lapply(olist, function(o) {
  
  out_btw <- rdrobust(y = diff_df[subset_select, o] %>% pull(!!o), 
                      x = diff_df$runvar[subset_select], c = 0,
                      covs = diff_df[subset_select, covs])
  out_btw_cdu <- rdrobust(y = diff_df[subset_select_cdu, o] %>% pull(!!o), 
                          x = diff_df$runvar[subset_select_cdu], c = 0,
                          covs = diff_df[subset_select_cdu, covs])
  out_btw_spd <- rdrobust(y = diff_df[subset_select_spd, o] %>% pull(!!o), 
                          x = diff_df$runvar[subset_select_spd], c = 0,
                          covs = diff_df[subset_select_spd, covs])
  
  ## Tidy
  
  r1 <- out_btw %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Full sample')
  r2 <- out_btw_cdu %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Municipal incumbent: CDU/CSU')
  r3 <- out_btw_spd %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Municipal incumbent: SPD')
  
  rbind(r1, r2, r3)
}) %>% 
  reduce(rbind) %>% 
  mutate(election = 'Federal\nelections')

## State elections ##

lt <- readRDS('data/data_state.rds') %>%
  filter(!is.na(treated)) %>%
  mutate(year = lubridate::year(date)) %>%
  mutate(state_id = substr(ags, 1, 2))

## Get list of states to use

lt <- read_rds("data/states_census.rds") %>%
  dplyr::select(applies_census, state_id, census_first_year) %>% 
  left_join(lt, .) %>%
  mutate(time_rel = year - census_first_year)

## This converts each election year to time period relative to treatment
## Errors can be ignored

periods_by_state <- lt %>%
  group_by(state_id) %>%
  summarise(l = list(unique(time_rel))) %>%
  unnest()
periods_by_state <- periods_by_state %>%
  group_by(state_id) %>%
  summarise(l_r = list(rank(l))) %>%
  unnest() %>% dplyr::select(-state_id) %>% 
  bind_cols(periods_by_state, .)
periods_by_state <- periods_by_state %>% 
  group_by(state_id) %>%
  mutate(l_r = l_r - max(l_r)) %>%
  filter(!is.na(l)) %>%
  rename(time_rel_period = l_r,
         time_rel = l)

## Merge this back to the main DF

lt <- lt %>%
  left_join(periods_by_state) %>%
  mutate(post = ifelse(time_rel_period > -1, 1, 0),
         treated_post = post * treated) %>% 
  filter(time_rel_period > -2)

## First differences

olist <- c("turnout")

diff_df <- pblapply(olist, function(o) {
  out <- lt %>%
    filter(time_rel_period > -2) %>%
    mutate(time_rel_period = time_rel_period + 2)  %>% 
    pivot_wider(values_from = o, names_from = 'time_rel_period', 
                id_cols = 'ags',
                names_prefix = 'o') %>%
    mutate(diff = o2  - o1) %>%  dplyr::select(ags, diff) 
  ## Rename
  colnames(out)[2] <- o
  
  ## Return this
  out
}) %>%
  reduce(left_join) %>%
  left_join(lt %>% dplyr::select(ags, pop_dec_09, applies_census,
                                 one_of(covs),
                                 state_id) %>%
              distinct(ags, .keep_all = T)) %>%
  mutate(runvar = (pop_dec_09 * -1) + 10000) %>%
  filter(applies_census == 1) %>%
  filter(!str_sub(ags, 1, 2) == '01') %>% ## Exclude SH
  filter(!(substr(ags, 1, 2) == '08')) ## Exclude BW

## Merge muni incumbent to this 

diff_df <- diff_df %>% 
  left_join(mu %>% 
              dplyr::select(ags, inc_party_prior_to_census) %>% 
              distinct(ags, .keep_all = T))

## Subsets, as above

subset_select <- !diff_df$state_id == '08' 
subset_select_cdu <- !diff_df$state_id == '08' & 
  diff_df$inc_party_prior_to_census == 'cdu_csu'
subset_select_spd <- !diff_df$state_id == '08' & 
  diff_df$inc_party_prior_to_census == 'spd'

## Optimal BWs estimated separately

o <- olist[1]

out_ltw_full <- lapply(olist, function(o) {
  
  out_ltw <- rdrobust(y = diff_df[subset_select, o] %>% pull(!!o), 
                      x = diff_df$runvar[subset_select], c = 0,
                      covs = diff_df[subset_select, covs])
  out_ltw_cdu <- rdrobust(y = diff_df[subset_select_cdu, o] %>% pull(!!o), 
                          x = diff_df$runvar[subset_select_cdu], c = 0,
                          covs = diff_df[subset_select_cdu, covs])
  out_ltw_spd <- rdrobust(y = diff_df[subset_select_spd, o] %>% pull(!!o), 
                          x = diff_df$runvar[subset_select_spd], c = 0,
                          covs = diff_df[subset_select_spd, covs])
  
  ## Done, state
  
  r1 <- out_ltw %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Full sample')
  r2 <- out_ltw_cdu %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Municipal incumbent: CDU/CSU')
  r3 <- out_ltw_spd %>% tidy_rd(3) %>% 
    mutate(outcome = o, 
           incumbent_muni = 'Municipal incumbent: SPD')
  
  rbind(r1, r2, r3)
}) %>% 
  reduce(rbind) %>% 
  mutate(election = 'State\nelections')

## Combine

r_full <- out_muni_full %>% 
  bind_rows(out_ltw_full) %>% 
  bind_rows(out_btw_full) %>% 
  mutate(outcome = ifelse(outcome == 'turnout_party', 'turnout', outcome)) %>% 
  mutate(outcome = 'Turnout')

## Prep for plotting 

r_full <- r_full %>% 
  mutate(outcome= recode(outcome, 
                         `incumbent` = 'Municipal incumbent',
                         `state. incumbent` = 'State incumbent',
                         `fed. incumbent` = 'Federal incumbent',
                         `agg_left` = 'Left-wing parties',
                         `agg_center` = 'Centrist parties',
                         `agg_right` = 'Right-wing parties',
                         `spd` = 'SPD',
                         `cdu_csu` = 'CDU/CSU'))

## Order

order_df_elec <- data.frame(election = c("Municipal\nelections", 
                                         "State\nelections", 
                                         "Federal\nelections"),
                            order_elec = rev(c(1, 2, 3)))

## Combine

r_full <- r_full %>% 
  left_join(order_df_elec) %>% 
  mutate(election = as.factor(election),
         election = fct_reorder(election, order_elec))


# Figure A.21: Effect on turnout ----

r_full %>% 
  mutate(incumbent_muni = fct_rev(incumbent_muni)) %>% 
  ggplot(aes(incumbent_muni, estimate)) +
  geom_hline(yintercept = 0, linetype = 'dotted') +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
                width = 0) +
  geom_point(shape = 21, fill = 'white') +
  facet_wrap(~election) +
  theme_bw() +
  coord_flip() +
  ylab('RD estimate (percentage points)') +
  xlab('')


